<template>
  <a-spin :spinning="confirmLoading">
    <!--- 给代码上打标识 --->
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="容器名称" v-bind="validateInfos.name">
            <a-input v-model:value="props.formData.name" placeholder=" 请输入容器名称" :disabled="disabled">
              <template #suffix>
                <a-tooltip title="如未输入容器名称,则容器名称为匿名容器">
                  <info-circle-outlined style="color: rgba(0, 0, 0, 0.45)" />
                </a-tooltip>
              </template>
            </a-input>
          </a-form-item>
        </a-col> 

        <!--- xx小时,xx分钟这样 --->
        <a-col :span="24">
          <a-form-item label="容器分配机时" v-bind="validateInfos.machineTime">
            <a-input-number v-model:value="props.formData.hourTime" min="0"/> 小时
            <a-input-number v-model:value="props.formData.minuteTime" /> 分钟
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="容器分配GPU卡数量" v-bind="validateInfos.gpuCardNumber">
            <a-input-number v-model:value="props.formData.gpuCardNumber" min="0"/> 张
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="描述" v-bind="validateInfos.description">
            <a-textarea label="描述" v-model:value="props.formData.description" :disabled="disabled" placeholder="请输入镜像描述" :rows="4" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, defineExpose, nextTick, defineProps, computed } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../VmContainer.api';
  import { Form } from 'ant-design-vue';
  // 定义从外部来的数据+限制类型+指定默认值
  // 当不传的时候,使用default进行兜底
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => {} },// 默认为空
    formBpm: { type: Boolean, default: true },
  });
  // 响应式包装
  const formRef = ref();// 
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);

  const { createMessage } = useMessage();
  // 这个ref是指做一个绑定,类似于vue2中return data(){}那一套东西
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    name: [{ required: true, message: '请输入容器名称!'},],
    description: [{ required: true, message: '请输入描述!'},],
    gpuCardNumber: [{ required: true, message: '请输入GPU卡数量!' }]
  };
  const { resetFields, validate, validateInfos } = useForm(props.formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(() => {
    if (props.formBpm === true) {
      return props.formData.disabled;
    }
    return props.formDisabled;
  });

  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(props.formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = props.formData;
    if (model.id) {
      isUpdate.value = true;  
    } 
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      } 
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }
  // 定义暴露出来的方法
  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 400px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px; 
  }

  .components-input-demo-size .ant-input {
    width: 200px;
    margin: 0 8px 8px 0;
  }

</style>
